//通用
function popup(title, url, w, h, id) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "error/404";
    }
    if (w == null || w == '') {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }
    layer.open({
        id: id,
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false,
        maxmin: true,
        shadeClose: true,
        shade: 0.4,
        title: title,
        content: url
    });
}

/**
 * 父窗口弹出
 * @param url
 * @param data
 * @param tableId
 */
function postAjaxre(url, data, tableId) {
    $.ajax({
        url: url,
        type: "post",
        data: data,
        dataType: "json", traditional: true,
        success: function (data) {
            if (data.flag) {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                window.parent.layui.table.reload(tableId);
                window.top.layer.msg(data.msg, {icon: 6, offset: 'rb', area: ['120px', '80px'], anim: 2});
            } else {
                layer.msg(data.msg, {icon: 5, offset: 'rb', area: ['120px', '80px'], anim: 2});
            }
        }
    });
}

function layerAjax(url, data, tableId, func) {
    var loadindex = -1;
    $.ajax({
        url: url,
        type: 'post',
        data: data,
        traditional: true,
        beforeSend: function (xhr) {
            loadindex = layer.load(1, {shade: [0.1, '#fff']});
        },
        success: function (d) {
            if ($.isFunction(func)) {
                func(d);
            } else {
                var index = parent.layer.getFrameIndex(window.name);
                if (d.flag) {
                    parent.layer.close(index);
                    window.parent.layui.table.reload(tableId);
                    window.top.layer.msg(d.msg, {icon: 6, offset: 'auto', anim: 2});
                } else {
                    layer.msg(d.msg, {icon: 5});
                }
            }
        },
        error: function (e) {
            layer.alert("发生错误，"+e.responseText, {icon: 6});
        },
        complete: function () {
            layer.close(loadindex);
        }
    });
}

function layerAjaxGet(url, data, tableId, func) {
    var timestr=new Date().getTime();
    var reurl=url;
    if(url.indexOf("?")<0){
        reurl=url+"?timestr="+timestr;
    }else{
        reurl= url+"&timestr="+timestr;
    }
    $.ajax({
        url: reurl,
        type: 'get',
        data: data,
        traditional: true,
        success: function (d) {
            if ($.isFunction(func)) {
                func(d);
            } else {
                var index = parent.layer.getFrameIndex(window.name);
                if (d.flag) {
                    parent.layer.close(index);
                    window.parent.layui.table.reload(tableId);
                    window.top.layer.msg(d.msg, {icon: 6, offset: 'auto', anim: 2});
                } else {
                    layer.msg(d.msg, {icon: 5});
                }
            }
        }, error: function (e) {
            layer.alert("发生错误", {icon: 6}, function (index) {
                layer.close(index)
            });
        }
    });
}
function layerAjaxGetIT(url, data, tableId, uploadFile,i,func) {
    var timestr=new Date().getTime();
    var reurl=url;
    if(url.indexOf("?")<0){
        reurl=url+"?timestr="+timestr;
    }else{
        reurl= url+"&timestr="+timestr;
    }
    $.ajax({
        url: reurl,
        type: 'get',
        data: data,
        async: false,
        traditional: true,
        success: function (d) {
            if ($.isFunction(func)) {
                func(d);
            } else {
                var index = parent.layer.getFrameIndex(window.name);
                if (d.flag) {
                    parent.layer.close(index);
                    // window.parent.layui.table.reload(tableId);
                    window.top.layer.msg(d.msg, {icon: 6, offset: 'auto', anim: 2});
                } else {
                    layer.msg(d.msg, {icon: 5});
                }
            }
        },fail: function(res){
            layer.msg(res.msg,{icon:2});
        }, error: function (e) {
            layer.alert("发生错误", {icon: 6}, function () {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
        },complete: function(){
            if(i < uploadFile.length-1){
                i++;
                initOneFileShow(uploadFile,i);
            }
        }

    });
}

function eleClick(active, ele) {
    $(ele).on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
}

function toolDelByFlag(id, list, flag) {
    var data = {id: id};
    if (flag != null) {
        data.flag = flag;
    }
    $.ajax({
        url: "del",
        type: "post",
        data: data,
        success: function (d) {
            if (d.flag) {
                window.top.layer.msg(d.msg, {icon: 6, offset: 'auto', anim: 2});
                layui.table.reload(list);
            } else {
                window.top.layer.msg(d.msg, {icon: 5, offset: 'auto', anim: 2});
            }
        }, error: function () {
            alert('error');
        }
    });
}

function toolDel(id, list) {
    toolDelByFlag(id, list, null);
}

// 系统外打开监理
function openProjInfo(){
    var projId = localStorage.getItem('turntopageprjid');
    var projName = localStorage.getItem('turntopageprjname');
    if(projId){
        parent.window._showtab('xm_v' + projId, '查看-'+projName, '/supervisor/projectManager/projectDetail?type=v&id=' + projId);
        localStorage.removeItem('turntopageprjid');
        localStorage.removeItem('turntopageprjname');
    }
}

// 系统外打开咨询
function openProZxInfo(){
    var projId = localStorage.getItem('turntopageprjid');
    var projName = localStorage.getItem('turntopageprjname');
    if(projId){
        parent.window._showtab('xm_v' + projId, '查看-'+projName, '/zx/projectManager/projectDetail?type=v&id=' + projId);
        localStorage.removeItem('turntopageprjid');
        localStorage.removeItem('turntopageprjname');
    }
}

// 系统外打开检测
function openProJcInfo(){
    var projId = localStorage.getItem('turntopageprjid');
    var projName = localStorage.getItem('turntopageprjname');
    if(projId){
        parent.window._showtab('xm_v' + projId, '查看-'+projName, '/jc/projectManager/projectDetail?type=v&id=' + projId);
        localStorage.removeItem('turntopageprjid');
        localStorage.removeItem('turntopageprjname');
    }
}

//点击表格图片显示大图
function showImg(e) {
    var imgSrc = $(e).attr('src');
    var w = ($(window).width() *0.8);
    var h = ($(window).height() *0.8);
    imgSrc=imgSrc;
    getImageWidth(imgSrc,function(imgW,imgH){
        if(imgW*1 > w*1){
            imgW = w*1;
        }
        if(imgH*1 > h*1){
            imgH = h*1;
        }
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: [imgW+"px",imgH+"px"],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: false,
            content: '<img src="'+imgSrc+'" height="'+imgH+'px" width="'+imgW+'px" />',
        });
    })
}

// 获取图片真实高度
function getImageWidth(url, callback) {
    var img = new Image();
    img.src = url;
    // 如果图片被缓存，则直接返回缓存数据
    if (img.complete) {
        callback(img.width, img.height);
    } else {
        img.onload = function () {
            callback(img.width, img.height);
        }
    }
}

